

import GameManager from "../game/GameManager";
import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import { GameEvent } from "../libs/GameEvent";
import Helper from "../libs/HelperMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIShop extends cc.Component {


    @property(cc.Node)
    stoshs: cc.Node = null;

    @property(cc.Node)
    rods: cc.Node = null;




    onLoad() {
    }

    onEnable() {
        GameEvent.on(Constants.StageEvent.RefreshShop, this.refreshShopList, this)
    }

    start() {
        this.refreshShopList();
    }

    async refreshShopList() {
        for (let i = 0; i < this.stoshs.childrenCount; i++) {
            let item = this.stoshs.children[i];
            // let img = item.getChildByName("img").getComponent(cc.Sprite);
            // img.spriteFrame = await Helper.loadRes(`image/stosh${i + 1}`, cc.SpriteFrame)
            let name = item.getChildByName("name").getComponent(cc.Label);
            name.string = Constants.StoshInfo[i].name;
            let gold = item.getChildByName("gold");
            gold.getComponentInChildren(cc.Label).string = Constants.StoshInfo[i].price.toString();
            let tip = item.getChildByName("tip");
            tip.getComponentInChildren(cc.Label).string = "已有" + GameData.instance.playStoshs[i] + "条";
            tip.active = GameData.instance.playStoshs[i] > 0;
        }

        for (let i = 1; i <= this.rods.childrenCount; i++) {
            let item = this.rods.children[i - 1];
            // let img = item.getChildByName("img").getComponent(cc.Sprite);
            // img.spriteFrame = await Helper.loadRes(`image/rod${i + 1}`, cc.SpriteFrame)
            let name = item.getChildByName("name").getComponent(cc.Label);
            name.string = Constants.RodInfo[i].tip;
            let gold = item.getChildByName("gold");
            gold.getComponentInChildren(cc.Label).string = Constants.RodInfo[i].price.toString();
            gold.active = GameData.instance.playRods[i - 1] <= 0;
            let tip = item.getChildByName("tip");
            tip.getComponentInChildren(cc.Label).string = "已有";
            if (GameData.instance.playRod == i) {
                tip.getComponentInChildren(cc.Label).string = "已选";
            }
            tip.active = GameData.instance.playRods[i - 1] > 0;
        }



    }

    onStosh(event, customEventData) {
        let index = parseInt(customEventData);
        if (!GameManager.instace) {
            GameData.instance.shopIsStosh = true;
            GameData.instance.shopIndex = index;
            Helper.showUI(Constants.UIName.UIGetTool);
        } else {
            if (GameData.instance.playStoshs[index - 1] > 0) {
                GameData.instance.playStosh = index;
                GameManager.instace.playerIsStosh = true;
                this.onClose()
            } else {
                GameData.instance.shopIsStosh = true;
                GameData.instance.shopIndex = index;
                Helper.showUI(Constants.UIName.UIGetTool);
            }
        }
    }

    onRod(event, customEventData) {
        let index = parseInt(customEventData);
        if (GameData.instance.playRods[index - 1] > 0) {
            GameData.instance.playRod = index;
            this.onClose()
        } else {
            GameData.instance.shopIsStosh = false;
            GameData.instance.shopIndex = index;
            Helper.showUI(Constants.UIName.UIGetTool);
        }



    }





    onClose() {
        GameData.instance.saveData();
        this.node.removeFromParent();
        this.node.destroy();
    }


    onDisable() {
        GameEvent.off(Constants.StageEvent.RefreshShop, this.refreshShopList, this)
    }
}
